package com.ivo.act.controller;

import com.ivo.act.common.result.PageResult;
import com.ivo.act.common.result.Result;
import com.ivo.act.common.utils.ResultUtil;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程模型管理
 * @author wj
 * @version 1.0
 */
@RestController
@RequestMapping("/processModeler")
public class ProcessModelerController {

    @Resource
    private RepositoryService repositoryService;

    @GetMapping("/modelers")
    public PageResult modeler() {
        List<ProcessDefinition> processDefinitionList = new ArrayList<>();
                repositoryService.createProcessDefinitionQuery()
                .latestVersion()
                .list();
        List list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for(ProcessDefinition processDefinition : processDefinitionList) {
            Map map = new HashMap<>();
            map.put("id", processDefinition.getId());
            map.put("ver", processDefinition.getVersion());
            map.put("key", processDefinition.getKey());
            map.put("name", processDefinition.getName());
            String date = sdf.format(repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult().
                    getDeploymentTime());
            map.put("deployment_date", date);
            list.add(map);
        }
        return ResultUtil.successPage(list);
    }

    @GetMapping("/diagram/{processDef_id}")
    public void diagram(@PathVariable String processDef_id, HttpServletResponse response) throws IOException {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDef_id).singleResult();

        Deployment deployment =repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult();

        InputStream in = repositoryService.getResourceAsStream(deployment.getId(), processDefinition.getResourceName());

        // 输出流程图
        ServletOutputStream outputStream = response.getOutputStream();
        byte[] b = new byte[2048];
        int len;
        while ((len = in.read(b, 0, b.length)) != -1) {
            outputStream.write(b, 0, len);
        }
    }

    @PostMapping("/publish")
    public Result publish(String xmlData, String resourceName) {
        if(!StringUtils.contains(resourceName,".bpmn"))  resourceName = resourceName + ".bpmn";
        repositoryService.createDeployment().addString(resourceName, xmlData).deploy();
        return ResultUtil.success();
    }
}
